<script>
export default {
  name: "SearchManage",
  data() {
    return {
      tableData: [],
      pageSize: 10,
      pageNum: 1,
      total: 0,
      keywords:'',
      notifications: [],
      unreadCount: 0,
      notificationDialogVisible: false,
      form: {
        id: '',
        petName: '',
        loseTime: '',
        location: '',
        ownerNo: '',
        loseType: ''
      },
      rules: {
        petName: [{ required: true, message: '请输入宠物名称', trigger: 'blur' }]
      }
    };
  },
  methods: {
    loadPost() {
      this.$axios.post(this.$httpUrl + '/search/listPageS', {
        pageSize: this.pageSize,
        pageNum: this.pageNum,
        param: { keywords: this.keywords }
      }).then(res=>res.data).then(res=>{
        if (res.code === 200)
        {
          this.tableData = res.data;
          this.total = res.total;
        }else{
          alert('获取数据失败')
        }
      });
    },
    async loadNotifications() {
      const userId = this.$store.state.user.no;
      const res = await this.$axios.get(`/notification/listSearch?userId=${userId}`); // 自动获取当前管理员
      if (res.data.code === 200) {
        this.notifications = res.data.data.filter(n => n.status === 0);
        this.unreadCount = this.notifications.length;
        this.notificationDialogVisible = true;
      }
    },

    async handleProcess(notification) {
      await this.$axios.post(`/notification/process/${notification.id}`);

      console.log("notification.id:",notification.id);
      // 通过 relatedId 更新寻宠记录状态
      if (notification.relatedId) {
        await this.$axios.post(`/search/markProcessed/${notification.relatedId}`);
      }
      console.log("notification.relatedId:",notification.relatedId);
      this.loadNotifications();
      this.$message.success("已处理");
    },

    resetParam(){
      this.keywords = ''
      this.loadPost()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
    del(id){//"删除"按钮方法
      this.$axios.get(this.$httpUrl + `/search/del?id=${id}`).then(res => {
        if (res.data.code === 200) {
          this.$message.success('删除成功！');
          this.loadPost();
        } else {
          this.$message.error('删除失败！');
        }
      }).catch(error => {
        console.error('删除请求失败:', error);
        this.$message.error('请求异常，请检查控制台');
      });
    },
  },
  mounted() {
    this.loadNotifications(); // 页面加载时检查通知
  },
  beforeMount() {
    this.loadPost();
  }

};
</script>

<template>
  <div>
    <div style="border-bottom: 10px;margin-top: 5px" >
      <el-input v-model="keywords" placeholder="宠物名称/发布人/位置" suffix-icon="el-icon-search" style="width: 200px"
                @keyup.enter.native="loadPost"></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="loadPost">查询</el-button>
      <el-button type="success" @click="resetParam">重置</el-button>
      <el-badge :value="unreadCount" v-if="unreadCount > 0" style="margin-left: 10px">
        <el-button @click="loadNotifications" class="el-icon-bell">新消息提醒</el-button>
      </el-badge>
    </div>
    <!-- 表格寻宠字段 -->
    <el-table :data="tableData" :header-cell-style="{background:'#d2bcad'}"
              border>
      <el-table-column prop="id" label="ID">
      </el-table-column>
      <el-table-column prop="ownerNo" label="发布人" >
      </el-table-column>
      <el-table-column prop="loseTime" label="丢失时间" >
      </el-table-column>
      <el-table-column prop="location" label="丢失地点" >
      </el-table-column>
      <el-table-column prop="petName" label="宠物名称" >
      </el-table-column>
      <el-table-column prop="phoneNum" label="联系电话" >
      </el-table-column>
      <el-table-column prop="loseType" label="宠物类别" >
      </el-table-column>
      <el-table-column prop="operator" label="操作" width="200">
        <template slot-scope="scope">
          <el-popconfirm
              confirm-button-text='好的'
              confirm-button-type="warning"
              cancel-button-text='不用了'
              icon="el-icon-info"
              icon-color="red"
              title="该操作不可逆,确定删除吗? "
              @confirm="del(scope.row.id)"
              style="margin-left: 5px">
            <el-button slot="reference" size="small" type="danger" >删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="待处理通知" :visible.sync="notificationDialogVisible">
      <el-table :data="notifications">
        <el-table-column prop="content" label="内容"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="handleProcess(scope.row)">已 知 晓！</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </div>
</template>